package com.atguigu.gulimall.order.controller;

import cn.hutool.core.lang.UUID;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.service.OrderService;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;


/**
 * 订单
 *
 * @author huguangwei
 * @email huguangwei@gmail.com
 * @date 2022-10-07 20:35:27
 */
@RestController
@RequestMapping("order/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    RabbitTemplate rabbitTemplate;

    /**
     * 发送消息
     */
    @GetMapping("/sendMsg")
    public R sendMsg(@RequestParam("num") Integer num) {
        for (int i = 0; i < num; i++) {
//            if (i % 2 == 0) {
//                OrderReturnReasonEntity reason = new OrderReturnReasonEntity();
//                reason.setId(1L);
//                reason.setName("哈哈-" + i);
//                reason.setSort(1);
//                reason.setCreateTime(new Date());
//                reason.setStatus(1);
//                rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java", reason,new CorrelationData(UUID.randomUUID().toString()));
//            } else {
//                OrderEntity order = new OrderEntity();
//                order.setOrderSn(UUID.randomUUID().toString());
//                rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java", order,new CorrelationData(UUID.randomUUID().toString()));
//            }
//            System.out.println("消息发送完成");
            OrderEntity order = new OrderEntity();
            order.setOrderSn(UUID.fastUUID().toString());
            rabbitTemplate.convertAndSend("hello-java-exchange", "hello.java", order, new CorrelationData(UUID.randomUUID().toString()));
        }
        return R.ok();
    }

    /**
     * 查询订单的状态信息
     */
    @GetMapping("/status/{orderSn}")
    public R getOrderStatus(@PathVariable("orderSn") String orderSn) {
        OrderEntity orderEntity = orderService.getOrderByOrderSn(orderSn);
        return R.ok().setData(orderEntity);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = orderService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 获取当前用户的订单列表和订单项
     *
     * @param params
     * @return
     */
    @PostMapping("/listWithItem")
    public R listWithItem(@RequestBody Map<String, Object> params) {
        PageUtils page = orderService.queryPageWithItem(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        OrderEntity order = orderService.getById(id);

        return R.ok().put("order", order);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody OrderEntity order) {
		orderService.save(order);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody OrderEntity order) {
		orderService.updateById(order);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
		orderService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
